package com.tpyule.tpclass.util;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/19 14:00
 */
public class NumberUtil {
    /**
     * double保留n为小数
     * @param number
     * @return
     */
    public static double getDouble(double number, int count){
        StringBuilder sb = new StringBuilder();
        sb.append("0.");
        for (int i = 0; i < count; i++) {
            sb.append("0");
        }
        NumberFormat nf = new DecimalFormat(sb.toString());
        return Double.parseDouble(nf.format(number));
    }

    /**
     * 数字字符串数组转数字数组
     * @param str eg:[1,2,3,4,5]
     * @return
     */
    public static int[] StringToIntArray(String str){
        int[] array ;
        String s1 = "";
        if (str == null||"".equals(str))
            return new int[0];
        else {
            if(str.indexOf('[')!=-1&&str.indexOf(']')!=-1)
                s1 = str.substring(1,str.length()-1);
            else  s1 = str;
            String[] sa = s1.split(",");
            array = new int[sa.length];
            for (int i = 0; i < sa.length; i++) {
                array[i] = Integer.parseInt(sa[i]);
            }
            return array;
        }
    }
}
